package ca.compsci.opent;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.PushbackReader;

import ca.compsci.opent.codegen.BytecodeGenerator;
import ca.compsci.opent.lexer.Lexer;
import ca.compsci.opent.node.Start;
import ca.compsci.opent.parser.Parser;
import ca.compsci.opent.semantics.SemanticAnalyzer;
import ca.compsci.opent.semantics.SymbolTable;

public class Main {
	public static void main(String[] args) throws Exception {
		if (args.length == 0) {
			System.err.println("No file specified!");
			System.exit(1);
		}
		File file = new File(args[0]);
		FileReader file_reader = new FileReader(file);
		Lexer lexer = new Lexer(new PushbackReader(new BufferedReader(file_reader)));
		Parser parser = new Parser(lexer);
		
		Start ast = parser.parse();
		SemanticAnalyzer analyzer = new SemanticAnalyzer();
		ast.apply(analyzer);
		
		SymbolTable table = analyzer.getTable();
		
		File dir = file.getParentFile();
		BytecodeGenerator bcgen = new BytecodeGenerator(dir, table);
		
		ast.apply(bcgen);
	}
}
